package com.example.fms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.AttrDto;
import com.example.fms.dto.req.GoodsReqDto;
import com.example.fms.dto.resp.GoodsRespDto;
import com.example.fms.dto.resp.GoodsRespOneDto;
import com.example.fms.dto.resp.UserRespDto;
import com.example.fms.entity.Categories;
import com.example.fms.entity.CategoriesAttributes;
import com.example.fms.entity.Goods;
import com.example.fms.entity.GoodsAttr;
import com.example.fms.mapper.CategoriesAttributesMapper;
import com.example.fms.mapper.CategoriesMapper;
import com.example.fms.mapper.GoodsAttrMapper;
import com.example.fms.mapper.GoodsMapper;
import com.example.fms.service.CategoriesService;
import com.example.fms.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;

    @Resource
    CategoriesAttributesMapper categoriesAttributesMapper;
    @Resource
    GoodsAttrMapper goodsAttrMapper;

    @Override
    public Page<GoodsRespOneDto> getGoodsPage(String query,  GoodsReqDto goodsReqDto) {
        Page<GoodsRespDto> page = new Page<>(goodsReqDto.getPageNum(),goodsReqDto.getPageSize());
        List<GoodsRespDto> goodsList = goodsMapper.getGoodsList(page, goodsReqDto);
        Page<GoodsRespOneDto> pageOne = new Page<>(goodsReqDto.getPageNum(),goodsReqDto.getPageSize());
        List<GoodsRespOneDto> goodsRespOneDtos = new ArrayList<>();
        if (goodsList != null) {
            goodsList.forEach(s->{
                GoodsRespOneDto goodsRespOneDto = new GoodsRespOneDto();
                goodsRespOneDto.setGoods_id(s.getGoodsId());
                goodsRespOneDto.setGoods_name(s.getGoodsName());
                goodsRespOneDto.setGoods_price(s.getGoodsPrice());
                goodsRespOneDto.setGoods_number(s.getGoodsNumber());
                goodsRespOneDto.setGoods_weight(s.getGoodsWeight());
                goodsRespOneDto.setGoods_state(s.getGoodsState());
                goodsRespOneDto.setAdd_time(s.getAddTime());
                goodsRespOneDto.setUpd_time(s.getUpdTime());
                goodsRespOneDto.setHot_number(s.getHotNumber());
                goodsRespOneDto.setIs_promote(s.getIsPromote());

                goodsRespOneDto.setName(s.getName());
                goodsRespOneDto.setGender(s.getGender());
                goodsRespOneDto.setAge(s.getAge());
                goodsRespOneDto.setBaseCity(s.getBaseCity());
                goodsRespOneDto.setEducation(s.getEducation());
                goodsRespOneDto.setWork(s.getWork());
                goodsRespOneDto.setId(s.getId());
                goodsRespOneDtos.add(goodsRespOneDto);
            });
        }
        pageOne.setRecords(goodsRespOneDtos);
        pageOne.setTotal(page.getTotal());
        return pageOne;

    }

    @Override
    public Boolean saveGoods(GoodsReqDto req) {
        Goods goods = new Goods();
        goods.setGoodsName(req.getGoods_name());
        goods.setGoodsPrice(req.getGoods_price());
        goods.setGoodsNumber(req.getGoods_number());
        goods.setGoodsWeight(req.getGoods_weight());
        goods.setGoodsCat(req.getGoods_cat());
        goods.setGoodsIntroduce(req.getGoods_introduce());
        goods.setGoodsState(0);
        goods.setAddTime(new Date());
        goods.setUpdTime(new Date());
        goods.setName(req.getName());
        goods.setGender(req.getGender());
        goods.setAge(req.getAge());
        goods.setBaseCity(req.getBaseCity());
        goods.setEducation(req.getEducation());
        goods.setWork(req.getWork());
        goodsMapper.insert(goods);
        return true;
    }

    @Override
    public GoodsRespOneDto getGoodsById(String id) {
        GoodsRespOneDto goodsRespOneDto = new GoodsRespOneDto();
        Goods goods = goodsMapper.selectById(id);
        if (goods != null) {
            goodsRespOneDto.setGoods_id(goods.getGoodsId());
            goodsRespOneDto.setGoods_name(goods.getGoodsName());
            goodsRespOneDto.setGoods_price(goods.getGoodsPrice());
            goodsRespOneDto.setGoods_number(goods.getGoodsNumber());
            goodsRespOneDto.setGoods_weight(goods.getGoodsWeight());
            goodsRespOneDto.setGoods_state(goods.getGoodsState());
            goodsRespOneDto.setAdd_time(goods.getAddTime());
            goodsRespOneDto.setUpd_time(goods.getUpdTime());

            goodsRespOneDto.setName(goods.getName());
            goodsRespOneDto.setGender(goods.getGender());
            goodsRespOneDto.setAge(goods.getAge());
            goodsRespOneDto.setBaseCity(goods.getBaseCity());
            goodsRespOneDto.setEducation(goods.getEducation());
            goodsRespOneDto.setWork(goods.getWork());

            goodsRespOneDto.setHot_number(goods.getHotNumber());
            LambdaQueryWrapper<GoodsAttr> qw = new LambdaQueryWrapper<>();
            qw.eq(GoodsAttr::getGoodsId,goods.getGoodsId());
            List<GoodsAttr> goodsAttrs = goodsAttrMapper.selectList(qw);
            List<AttrDto> attrDtos = new ArrayList<>();
            if (!CollectionUtil.isEmpty(goodsAttrs)) {
                LambdaQueryWrapper<CategoriesAttributes> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(CategoriesAttributes::getAttrId, goodsAttrs.stream().map(GoodsAttr::getAttrId).collect(Collectors.toList()));
                List<CategoriesAttributes> categoriesAttributes = categoriesAttributesMapper.selectList(queryWrapper);
                categoriesAttributes.forEach(categoriesAttribute -> {
                    AttrDto attrDto = new AttrDto();
                    attrDto.setAttr_id(categoriesAttribute.getAttrId());
                    attrDto.setAttr_value(categoriesAttribute.getAttrName());
                    attrDtos.add(attrDto);
                });
            }
            goodsRespOneDto.setAttrs(attrDtos);
        }
        return goodsRespOneDto;
    }

    @Override
    public Boolean putGoodsById(GoodsReqDto req, Long id) {
        Goods goods = new Goods();
        goods.setGoodsId(id);
        goods.setGoodsName(req.getGoods_name());
        goods.setGoodsCat(req.getGoods_cat());
        goods.setGoodsPrice(req.getGoods_price());
        goods.setGoodsNumber(req.getGoods_number());
        goods.setGoodsWeight(req.getGoods_weight());
        goods.setGoodsCat(req.getGoods_cat());
        goods.setGoodsIntroduce(req.getGoods_introduce());
        goods.setGoodsState(0);
        goods.setAddTime(new Date());
        goods.setUpdTime(new Date());

        goods.setName(req.getName());
        goods.setGender(req.getGender());
        goods.setAge(req.getAge());
        goods.setBaseCity(req.getBaseCity());
        goods.setEducation(req.getEducation());
        goods.setWork(req.getWork());
        goodsMapper.updateById(goods);
        List<AttrDto> attrs = req.getAttrs();
        if (!CollectionUtil.isEmpty(attrs)) {
            LambdaQueryWrapper<GoodsAttr> qw = new LambdaQueryWrapper<>();
            qw.eq(GoodsAttr::getGoodsId, goods.getGoodsId());
            goodsAttrMapper.delete(qw);
            attrs.forEach(attrDto -> {
                GoodsAttr goodsAttr = new GoodsAttr();
                goodsAttr.setGoodsId(goods.getGoodsId());
                goodsAttr.setAttrId(attrDto.getAttr_id());
                goodsAttrMapper.insert(goodsAttr);
            });
        }
        return true;
    }

    @Override
    public Boolean deleteGoodsById(Long id) {
        goodsMapper.deleteById(id);
        LambdaQueryWrapper<GoodsAttr> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsAttr::getGoodsId, id);
        goodsAttrMapper.delete(qw);
        return true;
    }
}
